
//
//  AppPageView.m
//  Helper
//
//  Created by nsm on 2019/4/18.
//  Copyright © 2019 nani. All rights reserved.
//

#import "AppPageView.h"

@interface AppPageView () <UITableViewDelegate>
@property (nonatomic, strong) NSMTableView *tableView;
@property (nonatomic, strong) NSMButton *emptyBtn;
@end

@implementation AppPageView
- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (!CGRectEqualToRect(self.tableView.frame, self.bounds)) {
        self.tableView.frame = self.bounds;
    }
}

- (UIScrollView *)pageScrollView {
    return self.tableView;
}

- (void)preferredSetting {
    [self.tableView beginRefreshing:RefreshHudDefault];
}

#pragma mark - 初始化
+ (instancetype)pageView:(NSString *)url param:(NSDictionary *)param {
    return [[self alloc] initWithFrame:CGRectZero url:url param:param];
}

- (instancetype)initWithFrame:(CGRect)frame url:(NSString *)url param:(NSDictionary *)param {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup:url param:param];
    }
    return self;
}

- (void)setup:(NSString *)url param:(NSDictionary *)param  {
    BOOL useBtn = [self preferredEmptyBtn];
    
    self.tableView.urlString = url;
    self.tableView.parameters = param;
    self.tableView.emptySetEnabled = !useBtn;
    self.tableView.headerRefreshEnabled = YES;
    self.tableView.footerRefreshEnabled = YES;
    
    @nsm_weak(self)
    self.tableView.sourceManager.classForCellAtIndexPath = ^Class(NSIndexPath *indexPath) {
        return NSMTableViewCell.class;
    };
    self.tableView.didRefreshingHeaderSuccess = ^(id responseObject) {
        @nsm_strong(self)
        id data = ((NSMResponseObject *)responseObject).data;
        self.tableView.sourceManager.dataArray = [NSMutableArray arrayWithArray:data[@"list"]];
        if (useBtn)
            self.tableView.tableFooterView = UIView.new;
        !self.onHeaderSuccess?:self.onHeaderSuccess(data);
    };
    self.tableView.didRefreshingFooterSuccess = ^(id responseObject) {
        @nsm_strong(self)
        id data = ((NSMResponseObject *)responseObject).data;
        [self.tableView.sourceManager.dataArray addObjectsFromArray:data[@"list"]];
        if (useBtn)
            self.tableView.tableFooterView = UIView.new;
        !self.onHeaderSuccess?:self.onHeaderSuccess(data);
    };
    self.tableView.didRefreshingHeaderEmpty = ^(id responseObject) {
        @nsm_strong(self)
        self.tableView.sourceManager.dataArray = @[].mutableCopy;
        self.tableView.imageForEmptySet = kEmptyDataImage;
        self.tableView.titleForEmptySet = kEmptyDataDesc(((NSMResponseObject *)responseObject).desc);
        if (useBtn) {
            self.emptyBtn.title = @"空空如也~";
            self.tableView.tableFooterView = self.emptyBtn;
        }
    };
    self.tableView.didRefreshingHeaderFailure = ^(NSError *error) {
        @nsm_strong(self);
        self.tableView.imageForEmptySet = kEmptyDataImage;
        self.tableView.titleForEmptySet = kEmptyDataDesc(@"亲,稍后再试");
        if (useBtn) {
            self.emptyBtn.title = @"亲,稍后再试";
            self.tableView.tableFooterView = self.emptyBtn;
        }
    };
    self.tableView.emptyRefreshingBlock = ^{
        @nsm_strong(self)
        [self.tableView beginRefreshing:RefreshHudDefault];
    };
    
    [self preferredSetting];
}

#pragma mark - setter/getter
- (UITableViewStyle)preferredTableViewStyle {
    return UITableViewStylePlain;
}

- (BOOL)preferredEmptyBtn {
    return NO;
}

- (NSMButton *)emptyBtn {
    if (!_emptyBtn) {
        _emptyBtn = NSMButton.new;
        _emptyBtn.size = CGSizeMake(kScreenWidth, kScreenWidth);
        _emptyBtn.imageSize = CGSizeMake(122, 100);
        _emptyBtn.spacing = 12;
        _emptyBtn.imagePosition = NSMButtonImagePositionTop;
        _emptyBtn.titleLabel.font = CustomFont(14);
        [_emptyBtn setTitleColor:RGBColor(200, 200, 200) forState:UIControlStateNormal];
        [_emptyBtn setImage:BundleImage(@"nsm_empty_tip@2x") forState:UIControlStateNormal];
        
        @nsm_weak(self)
        [_emptyBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
            @nsm_strong(self)
            [self.tableView beginRefreshing:RefreshHudDefault];
        }];
    }
    return _emptyBtn;
}

- (NSMTableView *)tableView {
    if (!_tableView) {
        _tableView = [[NSMTableView alloc] initWithFrame:self.bounds
                                                   style:[self preferredTableViewStyle]];
        _tableView.delegate = self;
        _tableView.dataSource = _tableView.sourceManager;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 0);
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 150;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [self addSubview:_tableView];
    }
    return _tableView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id obj = tableView.sourceManager.dataArray[indexPath.row];
    !self.didSelectItem?:self.didSelectItem(obj, indexPath);
}

#pragma mark - UITableViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    !self.pageScrollViewDidScroll?:self.pageScrollViewDidScroll(scrollView);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    !self.pageScrollViewWillBeginDragging?:self.pageScrollViewWillBeginDragging(scrollView);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    !self.pageScrollViewDidEndDecelerating?:self.pageScrollViewDidEndDecelerating(scrollView);
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    !self.pageScrollViewDidEndScrollingAnimation?:self.pageScrollViewDidEndScrollingAnimation(scrollView);
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    !self.pageScrollViewDidEndDragging?:self.pageScrollViewDidEndDragging(scrollView, decelerate);
}

@end
